package com.wenhao.interview.huawei;

import java.util.*;

/**
 * @author Wenhao Tong
 * @create 2021-07-21 19:54
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String list = scanner.next();
        String[] split = list.split("[\\[\\]\\,]");
        List<Integer> numbers = new ArrayList<>();
        for (int i = 0;i < split.length;i++) {
            if (!"".equals(split[i])) {
                numbers.add(Integer.parseInt(split[i]));
            }
        }
        Map<Integer, List<int[]>> map = new HashMap<>();
        for (int i = 0;i < numbers.size();i += 3) {
            int start = numbers.get(i);
            int end = numbers.get(i + 1);
            int weight = numbers.get(i + 2);
            if (map.containsKey(start)) {
                map.get(start).add(new int[]{end,weight});
            } else {
                List<int[]> temp = new ArrayList<>();
                temp.add(new int[]{end,weight});
                map.put(start,temp);
            }
        }
        for (Map.Entry<Integer,List<int[]>> node : map.entrySet()) {
            dfs(0,map,node.getKey());
        }
        System.out.println(max);
    }
    public static int max = 0;
    public static void dfs (int depth,Map<Integer, List<int[]>> map,int start) {
        if (!map.containsKey(start)) {
            max = max > depth ? max : depth;
            return;
        }
        List<int[]> next = map.get(start);
        if (next.size() == 0) {
            max = max > depth ? max : depth;
            return;
        }
        for (int[] nextNode : next) {
            dfs(depth + nextNode[1],map,nextNode[0]);
        }
    }
}
